#include "stm32f10x.h"
#include "LED.h"
#include "Delay.h"
uint8_t cnt = 0;

void Light_Interrput_Init(void)
{
    // enable gpio clock 
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

    // enable afio clock 
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

    // init gpio 

    GPIO_InitTypeDef GPIO_Init_Param;
    GPIO_Init_Param.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init_Param.GPIO_Pin = GPIO_Pin_14;
    GPIO_Init_Param.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(GPIOB, &GPIO_Init_Param);

    // init afio 
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource14);

    // init exti
    EXTI_InitTypeDef EXTI_Init_Param;
    EXTI_Init_Param.EXTI_Line = EXTI_Line14;
    EXTI_Init_Param.EXTI_LineCmd = ENABLE;
    EXTI_Init_Param.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_Init_Param.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
    EXTI_Init(&EXTI_Init_Param);
    // config nvic priority 
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    // init nvic (only init once)
    NVIC_InitTypeDef NVIC_Init_Param;
    NVIC_Init_Param.NVIC_IRQChannel = EXTI15_10_IRQn;
    NVIC_Init_Param.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init_Param.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_Init_Param.NVIC_IRQChannelSubPriority = 1;

    NVIC_Init(&NVIC_Init_Param);

}

void EXTI15_10_IRQHandler(void)
{
        // LED_Open();
        // Delay_ms(500);
        // LED_Close();
        // Delay_ms(500);
    if (EXTI_GetITStatus(EXTI_Line14) == SET)
    {
        // interrupt
        cnt++;

        EXTI_ClearITPendingBit(EXTI_Line14);
    }

}

uint8_t Get_Count(void)
{
    return cnt;
}